<template>
  <a-card class="j-address-list-right-card-box" :loading="cardLoading" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">

          <a-col :md="6" :sm="12">
            <a-form-item :label="$t('m.system.user.entity.realname')" style="margin-left:8px">
              <a-input placeholder="请输入姓名查询" v-model="queryParam.realname"></a-input>
            </a-form-item>
          </a-col>


          <a-col :md="6" :sm="12">
            <a-form-item :label="$t('m.system.user.entity.workNo')" style="margin-left:8px">
              <a-input placeholder="请输入工号查询" v-model="queryParam.workNo"></a-input>
            </a-form-item>
          </a-col>

          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
             <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 18px">{{ $t('m.common.button.query') }}</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="userId"
      :pagination="ipagination"
      :columns="columns"
      :dataSource="dataSource"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      @change="handleTableChange">
      <template slot="serialNum" slot-scope="text,record,index">
        {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
      </template>
    </a-table>

  </a-card>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { YcListMixin } from '@/mixins/YcListMixin'
  import YchConstant from '@/api/YchConstant'

  export default {
    name: 'SelectUserTreeRight',
    mixins: [YcListMixin],
    components: {},
    props: ['value'],
    data() {
      return {
        description: '用户信息',
        cardLoading: true,
        positionInfo: {},
        columns: [
          {
            title:this.$t('m.common.label.number'),
            key: 'rowIndex',
            dataIndex: '',
            width: 50,
            align: 'center',
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$t('m.system.org.entity.depName'),
            width: 120,
            align: 'center',
            dataIndex: 'departName'
          },
          {
            title: this.$t('m.system.user.entity.realname'),
            width: 120,
            align: 'center',
            dataIndex: 'realname'
          },
          /*{
            title: this.$t('m.system.user.entity.sex'),
            align: 'center',
            width: 80,
            dataIndex: 'sex_dictText'
          },*/
          {
            title: this.$t('m.system.user.entity.phone'),
            align: 'center',
            width: 120,
            dataIndex: 'phone'
          },
          {
            title: this.$t('m.system.user.entity.email'),
            align: 'center',
            width: 160,
            dataIndex: 'email'
          },
          {
            title: this.$t('m.system.user.entity.workNo'),
            width: 120,
            align: 'center',
            dataIndex: 'workNo'
          }
        ],
        selectedRowKeys: [],
        selectedRows: [],
        url: {
          list: YchConstant.PREFIX_SYSTEM + '/user/queryByOrgCodeForAddressList',
          listByPosition: YchConstant.PREFIX_SYSTEM + '/sysPosition/list'
        }
      }
    },
    watch: {
      value: {
        immediate: true,
        handler(orgCode) {
          this.dataSource = []
          this.loadData(1, orgCode)
        }
      }
    },
    created() {
      this.queryPositionInfo()
    },
    methods: {

      loadData(pageNum, orgCode) {
        if (!orgCode) {
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (pageNum === 1) {
          this.ipagination.current = 1
        }
        this.loading = true
        getAction(this.url.list, {
          orgCode,
          ...this.getQueryParams()
        }).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total
          }
        }).finally(() => {
          this.loading = false
          this.cardLoading = false
        })
      },

      searchQuery() {
        this.loadData(1, this.value)
      },
      searchReset() {
        this.queryParam = {}
        this.loadData(1, this.value)
      },

      handleTableChange(pagination, filters, sorter) {
        if (Object.keys(sorter).length > 0) {
          this.isorter.column = sorter.field
          this.isorter.order = 'ascend' === sorter.order ? 'asc' : 'desc'
        }
        this.ipagination = pagination
        this.loadData(null, this.value)
      },

      // 查询职务信息,待修改xiongda
      queryPositionInfo() {
        // getAction(this.url.listByPosition, { pageSize: 99999 }).then(res => {
        //   if (res.success) {
        //     let positionInfo = {}
        //     res.result.records.forEach(record => {
        //       positionInfo[record['code']] = record['name']
        //     })
        //     this.positionInfo = positionInfo
        //   }
        // })
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectionRows = selectedRows
      }
    }
  }
</script>
<style>
  .j-address-list-right-card-box .ant-table-placeholder {
    min-height: 46px;
  }
</style>
<style lang="less" scoped>
  .j-address-list-right-card-box {
    height: 100%;
    min-height: 300px;
  }
</style>
